import {
  Controller,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  UseInterceptors,
  UploadedFile,
  Put
} from '@nestjs/common'
import { FileInterceptor } from '@nestjs/platform-express/multer'
import { TransferService } from './transfer.service'
import { CreateTransferDto } from './dto/create-transfer.dto'
import { UpdateTransferDto } from './dto/update-transfer.dto'
import { ApiBody, ApiOperation, ApiParam, ApiTags } from '@nestjs/swagger'
import * as xlsx from 'xlsx'
import { HttpCode } from '@nestjs/common/decorators'
import { NoAuth } from 'src/role/decorators/no-auth.decorator'
import { HttpStatus } from '@nestjs/common/enums'
import { FResponse } from 'src/utils/reponse'
@NoAuth()
@ApiTags('调动')
@Controller('transfer')
export class TransferController {
  constructor(private readonly transferService: TransferService) {}

  @Post()
  @ApiOperation({ summary: '创建调动' })
  @ApiBody({ type: CreateTransferDto, description: '调动信息' })
  @HttpCode(HttpStatus.OK)
  async create(@Body() createTransferDto: CreateTransferDto) {
    return FResponse.success(
      await this.transferService.create(createTransferDto)
    )
  }

  @Post('upload')
  @ApiOperation({ summary: '批量创建调动' })
  @UseInterceptors(FileInterceptor('file'))
  async batchCreate(@UploadedFile() file) {
    const fileData = file.buffer
    const workbook = xlsx.read(fileData, { type: 'buffer' })
    const worksheet = workbook.Sheets[workbook.SheetNames[0]]
    const data: CreateTransferDto[] = xlsx.utils.sheet_to_json(worksheet)
    await Promise.all(data.map(item => this.transferService.create(item)))
    return FResponse.success('批量创建成功')
  }

  @Post('all')
  @ApiOperation({ summary: '获取所有调动' })
  @ApiBody({
    description: '调动信息'
  })
  async findAll(
    @Body()
    body: {
      page: number
      pageSize: number
      isDelete?: boolean
    }
  ) {
    return FResponse.success(
      await this.transferService.findAll({
        page: +body.page,
        pageSize: +body.pageSize,
        isDelete: body.isDelete
      })
    )
  }

  // 根据员工Id获取调薪
  @Post('staff/:id')
  @ApiOperation({ summary: '获取指定员工的调薪' })
  @ApiParam({ name: 'id', description: '员工ID' })
  async findByStaffId(
    @Param('id') id: string,
    @Body()
    body: {
      page: number
      pageSize: number
      isDelete?: boolean
    }
  ) {
    return FResponse.success(
      await this.transferService.findAll(
        {
          page: +body.page,
          pageSize: +body.pageSize,
          isDelete: body.isDelete
        },
        +id
      )
    )
  }

  @Patch(':id')
  @ApiOperation({ summary: '更新指定调动' })
  @ApiParam({ name: 'id', description: '调动ID' })
  @ApiBody({ type: UpdateTransferDto, description: '调动信息' })
  async update(
    @Param('id') id: string,
    @Body() updateTransferDto: UpdateTransferDto
  ) {
    return FResponse.success(
      await this.transferService.update(+id, updateTransferDto)
    )
  }

  // 批量删除
  @Delete()
  @ApiOperation({ summary: '批量删除调动' })
  @ApiBody({ type: [Number], description: '调动ID数组' })
  async batchRemove(@Body() ids: number[]) {
    return FResponse.success(await this.transferService.remove(ids))
  }

  // 批量恢复
  @Put('restore')
  @ApiOperation({ summary: '批量恢复调动' })
  @ApiBody({ type: [Number], description: '调动ID数组' })
  async batchRestore(@Body() ids: number[]) {
    return FResponse.success(await this.transferService.restore(ids))
  }
}
